const app = getApp();
import {getChatList,chatSpeakGood,delChatRecord} from "../../../config/api";
import util from "../../../utils/util";
// pages/new_index/chat_list/chat_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chat_list:[],
    page:1,
    limit:10,
    isEmpty:false,//是否显示暂无数据，默认不显示
  },



  delChatRecord(e){
    let that = this;
    console.log('item:',e.currentTarget.dataset.item);
    console.log('index::',e.currentTarget.dataset.index);
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '您确认删除该记录吗？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          util.apiRequest(delChatRecord,{id:item.id},null).then(r=>{
            console.log('删除结果：',r);
            wx.showToast({
              title: r.data.info,
              icon: 'loading',
              duration: 2000
            })
            that.getChatList();
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },


  playVideo(){
    let that = this;
    console.log('播放视频！');
    return;
  },



  //跳转到相应的详情页面
  toMarketDetail(e){
    let that = this;
    console.log('跳转到相应的详情页面：');
    wx.navigateTo({
       url:'/pages/new_index/chat_detail/chat_detail?id='+e.currentTarget.dataset.item.id
    });
  },

   //预览图片
   previewImg(e){
    let that = this;
    console.log('e.currentTarget.dataset.img:',e.currentTarget.dataset.img);
    wx.previewImage(
      {
        current: e.currentTarget.dataset.img,
        urls: e.currentTarget.dataset.imgs
      }
    );
  },

 //通用提示二次封装
 showDialogTitle(title){
  wx.showModal({
    title: '提示',
    content: title,
    showCancel:false,
    success (res) {
      if (res.confirm) {
        console.log('用户点击确定');
        return;
      } 
    }
  })
},
//展示相应的弹窗框提示二次封装
 //通用提示二次封装


  getChatList(){
    let that = this;
    let member = wx.getStorageSync('member');
    let params = {
      page:that.data.page,
      limit:that.data.limit,
      vollege_id:'',
      belong_town_id:app.globalData.town_id,
      belong_town_id:app.globalData.town_id,
    };
    if(member){
      params={
        page:that.data.page,
        limit:that.data.limit,
        idcard:member.idcard,
        vollege_id:member.vollege_id,
        vollege_id:app.globalData.vollege_id,
        belong_town_id:app.globalData.town_id,
      }
    };//如果村民改变参数值

    util.apiRequest(getChatList,params,null).then(r=>{
      console.log('获得的村聊列表为：',r.data.data);
      that.setData({
        chat_list:[...that.data.chat_list,...r.data.data]
      });
      if(that.data.page==1 && r.data.data.length==0){
         that.setData({
           isEmpty:true
         })
      }else{
        that.setData({
          isEmpty:false
        })
      }
      
      if(that.data.page!=1 && r.data.data.length==0){
        wx.showToast({
          title: '已经到底了',
          icon: 'loading',
          duration: 1000
        })
      }

    });
  },//获得村聊列表

  speakGood(e){
    let that = this;
    console.log('点赞的逻辑e.currenttarge.dataset.item:',e.currentTarget.dataset.item);
    console.log('点赞的逻辑e.currenttarge.dataset.index:',e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let member= wx.getStorageSync('member')
    if(!member){
      that.showDialogTitle(app.globalData.info);
      return;
    }
    let params = {
      idcard: member.idcard,
      talk_id: e.currentTarget.dataset.item.id,
      user_name: member.name,
      phone: member.mobile,
      address: '',
    };
    console.log('点赞提交的参数为params:',params);
    let objList= that.data.chat_list;
    util.apiRequest(chatSpeakGood,params).then(r=>{
      console.log('点赞结果：',r.data);
      if(item.thumb_sta==0){
         objList[index].thumb_sta=1;
         objList[index].thumbs_up+=1;
         objList[index].thumbs_up_list=[...objList[index].thumbs_up_list,...[{user_name: member.name, idcard: member.idcard}]];
      }else{
         objList[index].thumb_sta=0;
         objList[index].thumbs_up-=1;
         objList[index].thumbs_up_list.pop();
      }
      console.log('bojList',objList);
      that.setData({
        chat_list:objList
      });
    });

  },//点赞的逻辑



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getChatList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    that.setData({
      page:that.data.page+1
    });
    that.getChatList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})